package com.future.bantang.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.future.bantang.R;
import com.future.bantang.activity.RecommendDetailActivity;
import com.future.bantang.entity.SquareEntity;
import com.future.bantang.view.RoundPicText;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.List;


public class RecommendFragment extends BaseFragment {

    //TODO 完成后删除
    private static final String TAG = "RecommendFragment";

    // 控件
    @ViewInject(R.id.slv_one)
    private RoundPicText slvOne;

    @ViewInject(R.id.slv_two)
    private RoundPicText slvTwo;

    @ViewInject(R.id.slv_three)
    private RoundPicText slvThree;

    @ViewInject(R.id.slv_four)
    private RoundPicText slvFour;

    @ViewInject(R.id.slv_five)
    private RoundPicText slvFive;

    @ViewInject(R.id.slv_six)
    private RoundPicText slvSix;

    @ViewInject(R.id.slv_seven)
    private RoundPicText slvSeven;

    @ViewInject(R.id.slv_eight)
    private RoundPicText slvEight;

    //点击时传出去的数据id
    private String element_id = "";

    //点击时传出的数据title
    private String element_title = "";

    //副本数据id
    private String extend_id;

    List<RoundPicText> squareListViewList;

    //回传数据
    private SquareEntity.DataEntity.ModuleElementsEntity elementsEntity;

    public RecommendFragment() {
    }

    public static RecommendFragment newInstance(SquareEntity.DataEntity.ModuleElementsEntity elementsEntity) {
        RecommendFragment fragment = new RecommendFragment();
        Bundle args = new Bundle();
        args.putSerializable("data", elementsEntity);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            elementsEntity = (SquareEntity.DataEntity.ModuleElementsEntity)
                    getArguments().getSerializable("data");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recommend, container, false);

        ViewUtils.inject(this, view);

        initView(view);

        return view;
    }

    /**
     * 初始化控件
     *
     * @param view
     */
    @Override
    public void initView(View view) {
        squareListViewList = new ArrayList<>();

        squareListViewList.add(slvOne);
        squareListViewList.add(slvTwo);
        squareListViewList.add(slvThree);
        squareListViewList.add(slvFour);
        squareListViewList.add(slvFive);
        squareListViewList.add(slvSix);
        squareListViewList.add(slvSeven);
        squareListViewList.add(slvEight);

        for (int i = 0; i < squareListViewList.size(); i++) {
            squareListViewList.get(i).setIvPhoto(elementsEntity.getElements().get(i).getPhoto());
            squareListViewList.get(i).setTvTitle(elementsEntity.getElements().get(i).getTitle());
            squareListViewList.get(i).setTvSubTitle(elementsEntity.getElements().get(i).getSub_title());
        }
    }

    /**
     * 点击时跳转到显示的fragment
     *
     * @param v
     */
    @OnClick({R.id.slv_one, R.id.slv_two, R.id.slv_three,
            R.id.slv_four, R.id.slv_five, R.id.slv_six,
            R.id.slv_seven, R.id.slv_eight})
    public void showRecommend(View v) {
        switch (v.getId()) {
            case R.id.slv_one:
                element_id = elementsEntity.getElements().get(0).getId();
                element_title = elementsEntity.getElements().get(0).getTitle();
                extend_id = elementsEntity.getElements().get(0).getExtend();
                break;
            case R.id.slv_two:
                element_id = elementsEntity.getElements().get(1).getId();
                element_title = elementsEntity.getElements().get(1).getTitle();
                extend_id = elementsEntity.getElements().get(1).getExtend();
                break;
            case R.id.slv_three:
                element_id = elementsEntity.getElements().get(2).getId();
                element_title = elementsEntity.getElements().get(2).getTitle();
                extend_id = elementsEntity.getElements().get(2).getExtend();
                break;
            case R.id.slv_four:
                element_id = elementsEntity.getElements().get(3).getId();
                element_title = elementsEntity.getElements().get(3).getTitle();
                extend_id = elementsEntity.getElements().get(3).getExtend();
                break;
            case R.id.slv_five:
                element_id = elementsEntity.getElements().get(4).getId();
                element_title = elementsEntity.getElements().get(4).getTitle();
                extend_id = elementsEntity.getElements().get(4).getExtend();
                break;
            case R.id.slv_six:
                element_id = elementsEntity.getElements().get(5).getId();
                element_title = elementsEntity.getElements().get(5).getTitle();
                extend_id = elementsEntity.getElements().get(5).getExtend();
                break;
            case R.id.slv_seven:
                element_id = elementsEntity.getElements().get(6).getId();
                element_title = elementsEntity.getElements().get(6).getTitle();
                extend_id = elementsEntity.getElements().get(6).getExtend();
                break;
            case R.id.slv_eight:
                element_id = elementsEntity.getElements().get(7).getId();
                element_title = elementsEntity.getElements().get(7).getTitle();
                extend_id = elementsEntity.getElements().get(7).getExtend();
                break;
        }

        //普通模式跳转到RecommendDetailActivity, 工具具有适应性
        Intent intent = new Intent(getActivity(), RecommendDetailActivity.class);
        intent.putExtra("element_id", element_id);
        intent.putExtra("element_title", element_title);
        intent.putExtra("extend", extend_id);
        startActivity(intent);

    }

}
